import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.SwingConstants;

/**
 * 
 * Custom ID association class by Shiv Patel. Essentially a simplified hashmap
 * with additional methods and functionality. Should be mainly used for
 * assigning JLabels, but can be used for any generic ID to data association.
 * 
 * @author Shiv Patel
 * @version v1.0
 * 
 */
public class IDAssociator implements java.io.Serializable {

    private int ID;
    private Object data;

    private int x;
    private int y;
    private int width;
    private int height;

    private JLayeredPane mainPane;

    /**
     * constructor that takes in the player's id and associates it with a XYZ
     * location and size for an icon
     * 
     * @param ID
     *            the player's ID
     * @param x
     * @param y
     * @param width
     * @param height
     * @param inputPane
     * @param cond
     *            determines which layerd pane
     */
    public IDAssociator(int ID, int x, int y, int width, int height,
            JLayeredPane inputPane, boolean cond) {
        this.ID = ID;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        mainPane = inputPane;

        JLabel temporaryLabel = new JLabel("");
        temporaryLabel.setBounds(x, y, width, height);
        temporaryLabel.setHorizontalAlignment(SwingConstants.CENTER);
        if (cond) {
            mainPane.add(temporaryLabel, JLayeredPane.DRAG_LAYER);
        } else {
            mainPane.add(temporaryLabel, JLayeredPane.POPUP_LAYER);
        }

        data = temporaryLabel;
    }

    /**
     * getter for the data (most likely an icon)
     * 
     * @return
     */
    public Object getData() {
        return data;
    }

    /**
     * getter for the ID
     * 
     * @return
     */
    public int getID() {
        return ID;
    }

    /**
     * setter for the data
     * 
     * @param data
     */
    public void setData(Object data) {
        this.data = data;
    }

    /**
     * setter for the DI
     * 
     * @param ID
     */
    public void setID(int ID) {
        this.ID = ID;
    }

    /**
     * sets the data to a JLabel and makes it visible
     * 
     * @param url
     *            the url of the icon
     */
    public void setJLabelIcon(String url) {
        ((JLabel) data).setIcon(new ImageIcon(url));
        ((JLabel) data).setOpaque(true);
    }

    /**
     * sets the data to a JLabel and makes it visible
     * 
     * @param ico
     *            the icon that's being set
     */
    public void setJLabelIcon(Icon ico) {
        ((JLabel) data).setIcon(ico);
        ((JLabel) data).setOpaque(true);
    }

    /**
     * deletes the JLabel data and refreshes the pane
     */
    public void removeData() {
        mainPane.remove((JLabel) data);
        mainPane.revalidate();
        mainPane.repaint();
    }

    /**
     * getter for the x-location
     * 
     * @return
     */
    public int getX() {
        return x;
    }

    /**
     * getter for the y-location
     * 
     * @return
     */
    public int getY() {
        return y;
    }

    /**
     * getter for the width
     * 
     * @return
     */
    public int getWidth() {
        return width;
    }

    /**
     * getter for the height
     * 
     * @return
     */
    public int getHeight() {
        return height;
    }

    /**
     * getter for the icon (Data)
     * 
     * @return
     */
    public Icon getIcon() {
        return ((JLabel) data).getIcon();
    }

}
